//
//  AddressChooseVC.m
//  KuWoCan
//
//  Created by hwd on 17/3/14.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "AddressChooseVC.h"
#import "AddressChooseCell.h"
#import "AddressEditVC.h"
#import "NWAddressRequest.h"
#import "OrderCargoChooseVC.h"


@interface AddressChooseVC ()<UITableViewDelegate,UITableViewDataSource,AddressCellDelegate>

@end

@implementation AddressChooseVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self getData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)getData
{
    NWAddressRequest *request = [[NWAddressRequest alloc] init];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        if (succ)
        {
            _addressArr = [AddressModel arrayOfModelsFromDictionaries:result];
            [self setUpUI];
        }
        
    }];
    [request startRequestGetAllAddressWithUserId:[Singleton shareInstance].userId];
}


- (void)setUpUI
{
    for(UIView *view in [self.view subviews])
    {
        [view removeFromSuperview];
    }
    
    self.view.backgroundColor = KwBgColor;
    self.title = @"收货地址";
    
    //创建列表
    _tableview = [[TPKeyboardAvoidingTableView
                   alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    _tableview.backgroundColor = KwBgColor;
    _tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    _tableview.estimatedRowHeight = 40.0;
    _tableview.rowHeight = UITableViewAutomaticDimension;
    _tableview.delegate = self;
    _tableview.dataSource = self;
    [self.view addSubview:_tableview];
    [_tableview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@0);
        make.left.right.equalTo(@0);
        make.bottom.equalTo(@-55);
    }];
    
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [confirmBtn setBackgroundColor:[UIColor whiteColor]];
    [confirmBtn setTitleColor:KwBlueColor forState:UIControlStateNormal];
    [confirmBtn setImage:kImg(@"tianjia") forState:UIControlStateNormal];
    [confirmBtn setTitleEdgeInsets:UIEdgeInsetsMake(0, 10, 0, 0)];
    [confirmBtn setTitle:@"新增地址" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = KwFont(16.0);
    [confirmBtn addTarget:self action:@selector(addAddress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(@0);
        make.top.equalTo(_tableview.mas_bottom).offset(5);
        make.left.equalTo(@0);
        make.right.equalTo(@0);
    }];

}

- (void)addAddress
{
    AddressEditVC *vc = [[AddressEditVC alloc] init];
    vc.requestType = RequestTypeAddAddress;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)onTapLeftBarButton {
    if (self.reloadBlock) {
        self.reloadBlock();
    }
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark tableViewDelegate,DataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _addressArr.count;
}

//- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    if (section == 1)
//    {
//        return 15;
//    }
//    return 0;
//}
//
//- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    if (section == 1)
//    {
//        UIView *headView = [UIView new];
//        headView.backgroundColor = KwBgColor;
//        return headView;
//    }
//    return nil;
//}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *addressChoose_ID = @"AddressChooseCell";
    AddressChooseCell *cell;
    //选择地址
    cell = [tableView dequeueReusableCellWithIdentifier:addressChoose_ID];
    if (!cell)
    {
        cell = [[AddressChooseCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:addressChoose_ID];
    }
    cell.model = _addressArr[indexPath.row];
    cell.indexPath = indexPath;
    cell.delegate = self;
    return cell;
  
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if ([[CommonTool getLastViewControllerWithCurrentViewController:self] isKindOfClass:[OrderCargoChooseVC class]])
    {
        [self.navigationController popViewControllerAnimated:YES];
        AddressModel *model = _addressArr[indexPath.row];
        if (_block)
        {
            _block(model);
        }
    }
}

#pragma mark AddressChooseCellDelegate

- (void)deleteAddressWithIndexPath:(NSIndexPath *)indexPath
{
    AddressModel *model = _addressArr[indexPath.row];
    NWAddressRequest *request = [[NWAddressRequest alloc] init];
    [request startRequestDeleteAddressWithAId:model.id];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        [SVProgressHUD showSuccessWithStatus:@"删除成功"];
        [self getData];
    }];
}

- (void)editAddressWithIndexPath:(NSIndexPath *)indexPath
{
    AddressModel *model = _addressArr[indexPath.row];
    AddressEditVC *vc = [[AddressEditVC alloc] init];
    vc.model = model;
    vc.requestType = RequestTypeEditAddress;
    [self.navigationController pushViewController:vc animated:YES];
}

- (void)setDefaultAddressWithIndexPath:(NSIndexPath *)indexPath
{
    AddressModel *model = _addressArr[indexPath.row];
    NWAddressRequest *request = [[NWAddressRequest alloc] init];
    [request startRequestSetDefaultAddressWithUserId:[Singleton shareInstance].userId aId:model.id];
    [request completion:^(id result, NSString *message, id extension, BOOL succ) {
        [SVProgressHUD showSuccessWithStatus:@"设置成功"];
        [self getData];
    }];
}

@end
